﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAO
{
    public class LapThucDonDao
    {
        QL_HuongRungEntities db = new QL_HuongRungEntities();
        public List<MonAnPublic> LayDS_MonAn()
        {
            var monan = from m in db.MONAN select new MonAnPublic { MaMonAn = m.MaMonAn, TenMonAn = m.TenMonAn };
            return monan.ToList();
        } 

        public bool ThemThucDon(THUCDON_NGAY ThucDon)
        {
            try
            {
                db.THUCDON_NGAY.Add(ThucDon);
                db.SaveChanges();
                return true;
            }
            catch (Exception) { return false; }
        }
        public void CapNhatChiTiet(CHITIET_THUCDON ThucDon)
        {
            try
            {
               // THUCDON_NGAY td = db.THUCDON_NGAY.Find(ThucDon.MaThucDon);
               // td.CHITIET_THUCDON = ThucDon.CHITIET_THUCDON;
                db.CHITIET_THUCDON.Add(ThucDon);
                db.SaveChanges();
            }
            catch (Exception) { }
        }

        public List<ThucDonPublic> getWeek()
        {
            var week = from td in db.THUCDON_NGAY select new ThucDonPublic {Tuan = td.Tuan};
            return week.Distinct().ToList();
        }

        public List<ThucDonPublic> NgayTrongTuan(string tuan)
        {
            var ngay = from n in db.THUCDON_NGAY where tuan == n.Tuan select new ThucDonPublic { MaThucDon = n.MaThucDon,Tuan = n.Tuan, NgayTrongTuan = n.NgayTrongTuan, NgayLap = n.Ngay };
            return ngay.ToList();
        }
        public List<CHITIET_THUCDON> DanhSachThucDon(int mathucdon)
        {

            var ct = db.CHITIET_THUCDON.Where(p => p.MaThucDon == mathucdon).ToList();
            return ct;
        }
        public bool XoaChiTiet(int ct)
        {
            try
            {
                db.Database.ExecuteSqlCommand("DELETE FROM CHITIET_THUCDON  WHERE MaThucDon = {0}", ct);
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool XoaThucDon(THUCDON_NGAY td)
        {
            try
            {
                THUCDON_NGAY thucdon = db.THUCDON_NGAY.Find(td.MaThucDon);
                db.THUCDON_NGAY.Remove(thucdon);
                db.SaveChanges();
                return true;
            }
            catch (Exception)
            {
                return false;
            }
            
        }
    }
}
